package com.winit.openapi.ums.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.winit.common.query.Page;
import com.winit.common.spi.context.CommandContext;
import com.winit.erp.spi.bpartner.vo.Bpartner;
import com.winit.openapi.constants.ApiConstant;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.ums.spi.customer.ShipperConsigneeService;
import com.winit.ums.spi.customer.command.AddShipperConsigneeCommand;
import com.winit.ums.spi.customer.command.DeleteShipperConsigneeCommand;
import com.winit.ums.spi.customer.command.ModifyShipperConsigneeCommand;
import com.winit.ums.spi.customer.command.PageShipperConsigneeCommand;
import com.winit.ums.spi.customer.command.QueryShipperConsigneeCommand;
import com.winit.ums.spi.customer.vo.ShipperConsigneeVo;

/**
 * 收发货人
 * @author qibin.tang
 *
 */
@Controller
@RequestMapping(value = "/ums/shipperConsignee")
public class ShipperConsigneeController extends BaseController {

    @Resource
    private ShipperConsigneeService shipperConsigneeService;
    
    /**
     * 提单收发货人列表
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/pageShipperConsignee")
    public String pageShipperConsignee(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
    	PageShipperConsigneeCommand command = JSON.parseObject(requestMsg.getData().toString(),PageShipperConsigneeCommand.class);
    	command.setCtx(CommandContext.getContext());
    	Bpartner bpartner = CommandContext.getContext().get(ApiConstant.API_BPARTNER);
        command.setCustomerCode(bpartner.getValue());
        Page<ShipperConsigneeVo> list = shipperConsigneeService.pageShipperConsignee(command);
        Map<String, Object> data = new HashMap<String, Object>();
        Map<String, Object> page = new HashMap<String, Object>();
        page.put("pageNo", list.getPageable().getPageNumber());
        page.put("pageSize", list.getPageable().getPageSize());
        page.put("totalCount", list.total);
        data.put("list", list.content);
        data.put("pageParams", page);
        responseMsg.setData(data);
        return SUCCESS;
    }
    
    /**
     * 新增收发货人列表
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/addShipperConsignee")
    public String addShipperConsignee(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        AddShipperConsigneeCommand command = JSON.parseObject(requestMsg.getData().toString(),AddShipperConsigneeCommand.class);
        command.setCtx(CommandContext.getContext());
        Bpartner bpartner = CommandContext.getContext().get(ApiConstant.API_BPARTNER);
        command.setCustomerCode(bpartner.getValue());
        shipperConsigneeService.addShipperConsignee(command);
        return SUCCESS;
    }
    
    /**
     * 根据条件查询提单收发货人
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/queryShipperConsignee")
    public String queryShipperConsignee(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
    	QueryShipperConsigneeCommand command = JSON.parseObject(requestMsg.getData().toString(),QueryShipperConsigneeCommand.class);
        command.setCtx(CommandContext.getContext());
        Bpartner bpartner = CommandContext.getContext().get(ApiConstant.API_BPARTNER);
        command.setCustomerCode(bpartner.getValue());
        List<ShipperConsigneeVo> list = shipperConsigneeService.queryShipperConsignee(command);
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("shipperConsigneeList", list);
        responseMsg.setData(data);
        return SUCCESS;
    }
    
    /**
     * 修改收发货人列表
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/modifyShipperConsignee")
    public String modifyShipperConsignee(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
    	ModifyShipperConsigneeCommand command = JSON.parseObject(requestMsg.getData().toString(),ModifyShipperConsigneeCommand.class);
        command.setCtx(CommandContext.getContext());
        Bpartner bpartner = CommandContext.getContext().get(ApiConstant.API_BPARTNER);
        command.setCustomerCode(bpartner.getValue());
        shipperConsigneeService.modifyShipperConsignee(command);
        return SUCCESS;
    }
    
    /**
     * 删除收发货人列表
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/deleteShipperConsignee")
    public String deleteShipperConsignee(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
    	DeleteShipperConsigneeCommand command = JSON.parseObject(requestMsg.getData().toString(),DeleteShipperConsigneeCommand.class);
        command.setCtx(CommandContext.getContext());
        shipperConsigneeService.deleteShipperConsignee(command);
        return SUCCESS;
    }
    
}
